﻿using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media.Imaging;

namespace files_manage
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        //当前选中文件路径
        public MainWindow()
        {
            InitializeComponent();

            // 绑定TreeView控件的事件处理程序
            folderTreeView.SelectedItemChanged += FolderTreeView_SelectedItemChanged;

            // 初始化TreeView
            PopulateTreeView(@"C:\", folderTreeView);

            //绑定listview点击
            fileListView.SelectionChanged += fileListView_SelectionChanged;
        }

        private void PopulateTreeView(string rootDirectory, TreeView treeView)
        {
            // 清空TreeView
            treeView.Items.Clear();

            // 添加根节点
            DirectoryInfo rootDir = new DirectoryInfo(rootDirectory);
            TreeViewItem rootNode = CreateDirectoryNode(rootDir);
            treeView.Items.Add(rootNode);

            // 递归加载根节点的子目录
            PopulateDirectory(rootNode, 2);
        }

        private void PopulateDirectory(TreeViewItem parentNode, int maxDepth)
        {
            DirectoryInfo directory = (DirectoryInfo)parentNode.Tag;

            if (directory == null || maxDepth <= 0)
                return;

            try
            {
                foreach (DirectoryInfo subDir in directory.GetDirectories())
                {
                    TreeViewItem node = CreateDirectoryNode(subDir);
                    parentNode.Items.Add(node);
                    PopulateDirectory(node, maxDepth - 1);
                }
            }
            catch (UnauthorizedAccessException)
            {
                // 处理无权限访问的目录
            }
        }

        private TreeViewItem CreateDirectoryNode(DirectoryInfo directory)
        {
            TreeViewItem node = new TreeViewItem();
            node.Header = directory.Name;
            node.Tag = directory;
            return node;
        }

        private void FolderTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeView treeView = (TreeView)sender;
            TreeViewItem selectedNode = (TreeViewItem)treeView.SelectedItem;
            if (selectedNode != null)
            {
                DirectoryInfo directory = (DirectoryInfo)selectedNode.Tag;
                if (directory != null)
                {
                    string directoryPath = directory.FullName;
                    PopulateListView(directoryPath);
                }
            }
        }

        /*
         * 当前目录下的文件填充到ListView 
         */
        private void PopulateListView(string directoryPath)
        {
            // 清空ListView
            fileListView.Items.Clear();

            // 获取选定目录下的文件和文件夹
            DirectoryInfo directory = new DirectoryInfo(directoryPath);

            try
            {
                foreach (DirectoryInfo subDir in directory.GetDirectories())
                {
                    // 添加子目录到ListView
                    fileListView.Items.Add(new FileSystemItem { Name = subDir.Name, Type = "文件夹" });
                }

                foreach (FileInfo file in directory.GetFiles())
                {
                    // 添加文件到ListView
                    fileListView.Items.Add(new FileSystemItem { Name = file.Name, Type = "文件" });
                }
            }
            catch (UnauthorizedAccessException)
            {
                // 处理无权限访问的目录
            }
        }

        /*
         *  点击文件或文件夹 
         */
        private void fileListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListView listView = (ListView)sender;
            FileSystemItem selectedItem = (FileSystemItem)listView.SelectedItem;

            if (selectedItem != null)
            {
                string selectedFileName = selectedItem.Name;
                string selectedFileType = selectedItem.Type;

                // 获取当前选中目录的完整路径
                TreeViewItem selectedNode = (TreeViewItem)folderTreeView.SelectedItem;
                if (selectedNode != null)
                {
                    DirectoryInfo directory = (DirectoryInfo)selectedNode.Tag;
                    string directoryPath = directory.FullName;

                    string selectedFilePath = Path.Combine(directoryPath, selectedFileName);

                    if (selectedFileType == "文件")
                    {
                        // 如果是文件，根据文件类型执行不同操作
                        if (selectedFileName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
                        {
                            // 打开txt文件
                            Process.Start("notepad.exe", selectedFilePath);
                        }
                        else if (selectedFileName.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
                        {
                            // 运行exe文件
                            Process.Start(selectedFilePath);
                        }
                        else
                        {
                            // 其他文件类型，提示不支持
                            MessageBox.Show("不支持的文件类型");
                        }
                    }
                }
            }
        }


    }

    // 定义用于填充ListView的数据模型
    public class FileSystemItem
    {
        public string Name { get; set; }
        public string Type { get; set; }
    }

}
